package com.antoco.operation.bigscreen.service.autogeneration.impl;

import com.antoco.operation.bigscreen.repository.PaizanNumTjRepository;
import com.antoco.operation.bigscreen.model.entity.PaizanNumTj;
import com.antoco.operation.bigscreen.model.entity.TjIns;
import com.antoco.operation.bigscreen.service.autogeneration.intf.PaizanNumTjService;
import com.antoco.operation.bigscreen.utils.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class PaizanNumTjServiceImpl extends AbstractModelServiceImpl implements PaizanNumTjService {

    @Autowired
    private PaizanNumTjRepository repository;

    @Override
    public PaizanNumTj save(PaizanNumTj entity) {
        TjIns tjIns = BeanUtils.saveTjIns(PaizanNumTj.class);
        entity.setTjInsId(tjIns.getId());
        return repository.save(entity);
    }

    @Override
    public List<PaizanNumTj> save(List<PaizanNumTj> entities) {
        TjIns tjIns = BeanUtils.saveTjIns(PaizanNumTj.class);
        for (PaizanNumTj entity : entities) {
            entity.setTjInsId(tjIns.getId());
        }
        return (List<PaizanNumTj>) repository.saveAll(entities);
    }

    @Override
    public void deleteById(String id) {
        repository.deleteById(id);
    }

    @Override
    public Optional<PaizanNumTj> findById(String id) {
        return repository.findById(id);
    }

    @Override
    public List<PaizanNumTj> findAll() {
        return (List<PaizanNumTj>) repository.findAll();
    }

    @Override
    public Page<PaizanNumTj> findAll(Pageable pageable) {
        Page<PaizanNumTj> entityPage = repository.findAll(pageable);
        List<PaizanNumTj> entities = entityPage.getContent();
        return new PageImpl<>(entities, pageable, entityPage.getTotalElements());
    }

    @Override
    public PaizanNumTj update(PaizanNumTj entity, String id) {
        Optional<PaizanNumTj> optional = findById(id);
        if (optional.isPresent()) {
            return save(entity);
        }
        return null;
    }
}